import { Modal } from 'ant-design-vue'
import { useAccountStore } from '@/stores/account'
import { createReservation, getCoreClaimDetailUrl, getMyMSHHospitalUrl } from '@/api/home'

export function useOpenEcardPage(employeeId, insuredId) {
  const params = {
    employeeId,
    insuredId
  }
  const queryString = Object.keys(params)
    .map((key) => `${key}=${encodeURIComponent(params[key])}`)
    .join('&')
  const baseUrl = `${import.meta.env.VITE_ECARD_URL}`
  const fullUrl = `${baseUrl}?${queryString}`
  const url = window.open(fullUrl)
}
/**
 *
 * @param operation String   创建预约：01 预约查询：02 医院详情：03
 */
async function fetchReservationLink(info) {
  let res = null
  let data = {
    insuredId: info.insuredId,
    memberId: info.member_id,
    grpContNo: info.grpContNo,
    grpPlanNo: info.grpPlanCode,
    operation: info.operation,
    insuredEffectiveDate: info.insuredEffectiveDate,
    insuredExpireDate: info.insuredExpireDate,
    providerId: info.providerId
  }
  try {
    res = await createReservation(data)
  } catch {}
  if (res?.isSuccess) {
    window.open(res.data)
  }
}
function judgeHospitalStatus(info) {
  const hospitalMessage = '当前选择的医院预约开关已关闭，请确认继续下一步还是返回重新选择'
  if (info.source == 'hospitalList' && info.hospitalStatusNeedTip) {
    Modal.confirm({
      keyboard: false,
      title: '提醒',
      okText: '继续',
      cancelText: '返回',
      content: hospitalMessage,
      onOk() {
        fetchReservationLink(info)
      },
      onCancel() {}
    })
    return
  }
  fetchReservationLink(info)
}
/**
 *
 * operation
 *  医院弹框-创建预约 01 ，需要confirm
 *  个人主页-预约查询 02， 无需confirm
 *  个人主页-创建预约 01， 需confirm
 *  个人主页-短信发送 05， 无需confirm
 *  医院弹框-预约医院 04， 需confirm
 */
export async function useOpenReservationPage(info) {
  const message = '当前选择的被保人已过保期，请确认继续下一步还是返回重新选择'
  const noNeedConfirmArr = ['02', '05']
  // 预约查询不需要判断被保人是否已过期
  const needShowConfirm = !noNeedConfirmArr.includes(info.operation)
  if (info.enrollmentStatus == '已过期' && needShowConfirm) {
    Modal.confirm({
      keyboard: false,
      title: '提醒',
      okText: '继续',
      cancelText: '返回',
      content: message,
      onOk() {
        judgeHospitalStatus(info)
      },
      onCancel() {}
    })
    return
  }
  judgeHospitalStatus(info)
}
/**
 * 点击跳转到预授权核心详情页面或 福利核心理赔详情页面。
 * @param {type} String   preAuth:预授权 claimDetail:福利理赔详情
 */
export async function useCorePage(params) {
  const accountStore = useAccountStore()
  const session = await accountStore.getSession()
  let res = null
  try {
    res = await getCoreClaimDetailUrl(params)
  } catch {}
  if (res?.isSuccess) {
    const coreUrl = `${res.data}${session}`
    window.open(coreUrl)
  }
}
/**
 * 医院列表-点击医院名称后，打开预约系统的医院查询页面
 */
export async function useHospitalDetailPage(params) {
  fetchReservationLink(params)
}
/**
 * 个人主页-直付医院列表
 */
async function getPaymentHospitalsUrl(params) {
  const data = {
    insuredId: params.insuredId,
    employeeId: params.member_id,
    customId: params.customer_id
  }
  let res = null
  try {
    res = await getMyMSHHospitalUrl(data)
  } catch {}
  if (res?.isSuccess) {
    const coreUrl = `${res.data}`
    window.open(coreUrl)
  }
}
export async function useDirectPaymentHospitals(params) {
  const message = '当前选择的被保人已过保期，请确认继续下一步还是返回重新选择'
  if (params.enrollmentStatus == '已过期') {
    Modal.confirm({
      keyboard: false,
      title: '提醒',
      okText: '继续',
      cancelText: '返回',
      content: message,
      onOk() {
        getPaymentHospitalsUrl(params)
      },
      onCancel() {}
    })
    return
  }
  getPaymentHospitalsUrl(params)
}
